// strngbad.cpp -- StringBad class methods
#include <cstring> // string.h for some
#include "49StringBad.hpp"
using std::cout;

// initializing static class member
int StringBad::num_strings = 0;
// 用于计数类的个数

// class methods

// construct StringBad from C string
StringBad::StringBad(const char *s)
{
    len = std::strlen(s);    // set size
    str = new char[len + 1]; // allot storage
    // 这里使用的new的方式进行分配内存,之后的析构函数使用delete[]删除内存
    std::strcpy(str, s);     // initialize pointer
    num_strings++;           // set object count
    cout << num_strings << ": \"" << str
         << "\" object created\n"; // For Your Information
}

StringBad::StringBad() // default constructor
{
    len = 4;
    str = new char[4];
    std::strcpy(str, "C++"); // default string
    num_strings++;
    cout << num_strings << ": \"" << str
         << "\" default object created\n"; // FYI
}

StringBad::~StringBad() // necessary destructor
{
    cout << "\"" << str << "\" object deleted, "; // FYI
    --num_strings;                                // required
    cout << num_strings << " left\n";             // FYI
    delete[] str;                                 // required
}

std::ostream &operator<<(std::ostream &os, const StringBad &st)
{
    os << st.str;
    return os;
}
